module.exports = function(app, settings){
	var log4js 	 = require('log4js')
		, _				 = require('lodash')
		, debug 	 = require('debug')('qywx-api')
		, webRequest  = require('request')
    , template    = require('es6-template-strings')
		, util 		 = require('util')
		, express  = require('express');


	var logger 	    = log4js.getLogger("qywx-api")
		, router 	    = express.Router();


	var GET_EMPLOYEES_URL
		= 'https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=${accessToken}'
      +'&department_id=${departmentId}&fetch_child=${fetchChild}&status=${status}';

	var GET_EMPLOYEES_INFO_URL
		= 'https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=${accessToken}&userid=${userid}';

	var GET_USERINFO_URL
		= 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=${accessToken}&code=${authCode}';

	router.route('/getuserinfo')
		.get(function(req, res, next){
			console.log("getuserinfo: ", req.originalUrl);
			var corpId = res.locals.corpId;

			var authCode = req.query.auth_code; //Authorizaiton Code
			var targetUrl = template(GET_USERINFO_URL
						, { accessToken: app.locals.accessToken[corpId].accessToken, authCode: authCode });

			debug("getuserinfo url: ", targetUrl);

			webRequest.get(targetUrl, function(error, response, body){
				if(error){
					debug("debug - ", error);
					logger.error(error);
					return res.json({
						success: false
						, errors: [{
							code: error.errcode
							, message: error.errmsg
							, internalError: error
						}]
					})
				}
				var data = JSON.parse(body);
				debug(" console.log ", body);
				logger.debug(" response - ", body);

				if(!data){
					return res.status(500).json({
						success: false,
						errCode: data.errcode,
						errMsg: data.errmsg
					});
				}

				return res.json(data);
			});
		});

	router.route("/:userid")
	 	.get(function(req, res, next){
			var userid = req.params.userid;
			var corpId = res.locals.corpId;

			var targetUrl = template(GET_EMPLOYEES_INFO_URL
						, { accessToken: app.locals.accessToken[corpId].accessToken, userid: userid });

			webRequest.get(targetUrl, function(error, response, body){
				if(error){
					logger.error(error);
					return res.json({
						success: false
						, errors: [{
							code: error.errcode
							, message: error.errmsg
							, internalError: error
						}]
					})
				}
				var data = JSON.parse(body);
				debug(" console.log ", body);
				if(data.errcode != 0){
					return res.status(500).json({
						success: false,
						errCode: data.errcode,
						errMsg: data.errmsg
					});
				}

				return res.json(data);
			});
	});

	router.route("/")
	 	.get(function(req, res, next){
			var departmentId = req.query.departmentId || 1
        , fetchChild = req.query.fetchChild || 0
        , status = req.query.status || 4;
		var corpId = res.locals.corpId;

		var targetUrl = template(GET_EMPLOYEES_URL
					, { accessToken: app.locals.accessToken[corpId].accessToken
              , departmentId: departmentId, fetchChild: fetchChild, status: status });

		webRequest.get(targetUrl, function(error, response, body){
			if(error){
				logger.error(error);
				return res.json({
					success: false
					, errors: [{
						code: error.errcode
						, message: error.errmsg
						, internalError: error
					}]
				})
			}
			var data = JSON.parse(body);
			debug(" console.log ", body);
			return res.json(data);
		});
	});

	app.use('/v1/employees', router);
}
